package com.carl.mall.search.controller;


import com.alibaba.fastjson.JSON;
import com.carl.common.dto.es.SkuESModel;
import com.carl.common.exception.BizCodeEnum;
import com.carl.common.utils.R;
import com.carl.mall.search.service.ElasticSaveService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 上架
 */
@Slf4j
@RestController
@RequestMapping("/search/save")
public class ElasticSaveController {

    @Autowired
    private ElasticSaveService elasticSaveService;
    /**
     * 存储商品上架信息到ElasticSearch服务的接口中
     */
    @PostMapping("/product")
    public R productStatusUp(@RequestBody List<SkuESModel> skuESModels){
        if (log.isInfoEnabled()){
            log.info("ElasticSaveController.productStatusUp.skuESModels:{}", JSON.toJSONString(skuESModels));
        }
        Boolean flag = false;
        try {
            flag = elasticSaveService.productStatusUp(skuESModels);
        } catch (Exception e) {
            log.error("ElasticSearch商品上架错误:{}",e);
            return R.error(BizCodeEnum.PRODUCT_UP_EXCEPTION.getCode(),BizCodeEnum.PRODUCT_UP_EXCEPTION.getMsg());
        }
        if (flag){
            return R.ok();
        }
        return R.error(BizCodeEnum.PRODUCT_UP_EXCEPTION.getCode(),BizCodeEnum.PRODUCT_UP_EXCEPTION.getMsg());
    }













}
